package vn.edu.fpt.xml.hrm.common;


import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Cimela
 */
public class XMLUlti {
    public static Document createDOMTree(String path) throws SAXException, IOException{
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            File file = new File(path);
            Document doc = db.parse(file);
            return doc;
        } 
        catch (ParserConfigurationException ex) {
            ex.printStackTrace();
        }        return null;
    }
    public static void writeXML(Node node, String outputFile){
        try {
            TransformerFactory tff = TransformerFactory.newInstance();
            Transformer trans = tff.newTransformer();
            Source src = new DOMSource(node);
            File file = new File(outputFile);
            Result result = new StreamResult(file);
            trans.setOutputProperty(OutputKeys.INDENT,"yes");
            trans.transform(src, result);

        } catch (TransformerException ex) {
            ex.printStackTrace();
        }
    }
}
